package arrays;

import interfaces.QueueStackDeque;
import exceptions.NoSuchElementException;

public class ArrayQueue  implements QueueStackDeque<String>{
	/**
	 * The array queue
	 */
	String[] a;
	/**
	 * The index of the head
	 */
	int j;
	/**
	 * Number of elements in the array queue
	 */
	int n;
	
	/**
	 * Creates a new instance of {@link ArrayQueue}
	 */
	public ArrayQueue (){
		a= new String[0];
		j=0;
		n=0;
	}
	@Override
	public boolean add(String x) {
		if(n+1>a.length)resize();
		a[(j+n)%a.length]=x;
		n++;
		return true;
	}
	@Override
	public String remove() throws NoSuchElementException {
		if(n==0)
			throw new NoSuchElementException();
		String x=a[j];
		j=(j+1)%a.length;
		n--;
		if(a.length>3*n)
			resize();	
		return x;
	}
	@Override
	public void resize() {
		String[] b=new String[Math.max(1, 2*n)];
		for(int k=0; k<n; k++){
			b[k]=a[(j+k)%a.length];

		}
		a=b;
		j=0;
		
	}
	@Override
	public int length() {
		return this.a.length;
	}
	
	
	
	

}
